package cn.com.infcn.monitor.task.web;

import java.util.ArrayList;
import java.util.List;

import cn.com.infcn.monitor.bean.IFCHttpRequest;
import cn.com.infcn.monitor.bean.IFCHttpRequestCounter;
import cn.com.infcn.monitor.filter.MonitoringFilter;
import cn.com.infcn.monitor.task.BaseTask;
import cn.com.infcn.monitor.util.IFCBeanUtils;
import cn.com.infcn.monitor.web.Counter;
import cn.com.infcn.monitor.web.RequestCounter;
import cn.com.infcn.monitor.web.RequestCounterAggregation;

public class HttpRequestTask implements BaseTask {

	@Override
	public Object execute() throws Exception {
		return httpRequest();
	}

	public IFCHttpRequest httpRequest() {
		Counter counter = MonitoringFilter.getHttpCounter();
		if (counter == null) {
			return null;
		}
		try {
			RequestCounterAggregation requestCounterAggregation = new RequestCounterAggregation(counter);

			RequestCounter globalRequest = requestCounterAggregation.getGlobalRequest();
			RequestCounter warningRequest = requestCounterAggregation.getWarningRequest();
			RequestCounter severeRequest = requestCounterAggregation.getSevereRequest();
			List<RequestCounter> list = requestCounterAggregation.getRequests();

			IFCHttpRequestCounter ifcglobal = new IFCHttpRequestCounter();
			IFCHttpRequestCounter ifcwarning = new IFCHttpRequestCounter();
			IFCHttpRequestCounter ifcsevere = new IFCHttpRequestCounter();
			List<IFCHttpRequestCounter> ifclist = new ArrayList<IFCHttpRequestCounter>();

			IFCBeanUtils.copyProperties(globalRequest, ifcglobal);
			IFCBeanUtils.copyProperties(warningRequest, ifcwarning);
			IFCBeanUtils.copyProperties(severeRequest, ifcsevere);

			IFCHttpRequestCounter ifcHttpRequestCounter = null;
			for (RequestCounter requestCounter : list) {
				ifcHttpRequestCounter = new IFCHttpRequestCounter();
				IFCBeanUtils.copyProperties(requestCounter, ifcHttpRequestCounter);
				ifclist.add(ifcHttpRequestCounter);
			}

			IFCHttpRequest httpRequest = new IFCHttpRequest();
			httpRequest.setGlobalRequestCounter(ifcglobal);
			httpRequest.setWarningRequestCounter(ifcwarning);
			httpRequest.setSevereRequestCounter(ifcsevere);
			httpRequest.setRequestCounterList(ifclist);

			return httpRequest;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
